package com.kgc.controller;
import com.kgc.bean.Jek;
import com.kgc.common.Dto;
import com.kgc.common.DtoUtil;
import com.kgc.common.EmptyUtils;
import com.kgc.common.MD5;
import com.kgc.config.T1;
import com.kgc.pojo.DmUser;
import com.kgc.service.user.DmUserService;
import com.kgc.utils.PhoneOrEmailRegex;
import com.kgc.utils.RedisKeys;
import com.kgc.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
@RestController
@RequestMapping("/api")
public class DmUserController {
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private DmUserService dmUserService;
    @Autowired
    private T1 t1;
    @Autowired
    private PhoneOrEmailRegex phoneOrEmailRegex;
    @RequestMapping(value = "/getDmUserById", method = RequestMethod.POST)
    public DmUser getDmUserById(@RequestParam("id") Long id) throws Exception {
        return dmUserService.getDmUserById(id);
    }

    @RequestMapping(value = "/p/login", method = RequestMethod.POST)
    public Dto login(@RequestBody DmUser dmUser) throws Exception {
        dmUser.setPassword(MD5.getMd5(dmUser.getPassword(), 32));
        Map<String, Object> map = new HashMap<>();
        map.put("phone", dmUser.getPhone());
        map.put("password", dmUser.getPassword());
        List<DmUser> dmUserListByMap = dmUserService.getDmUserListByMap(map);
        if (dmUserListByMap.size() == 0) {
            return DtoUtil.returnFail("账号或密码错误", "1006");
        }
        DmUser dmUser1 = dmUserListByMap.get(0);
        redisUtils.set(RedisKeys.getSysConfigKey(dmUser1.getPhone()), dmUser1);
        System.out.println("ip--------------------："+ t1.getHost());
        return DtoUtil.returnSuccess("登录成功", dmUser1);
    }
    @RequestMapping(value = "/v/loadCurrentUserByToken", method = RequestMethod.POST)
    public Dto loadCurrentUserByToken(@RequestParam String key) {
        String key1 = RedisKeys.getSysConfigKey(key);
        DmUser dmUser = redisUtils.get(key1, DmUser.class);
        if (dmUser != null) {
            return DtoUtil.returnDataSuccess(dmUser);
        } else
            return DtoUtil.returnFail("登录失效，请先登录", "11111");
    }

    @RequestMapping(value = "/p/checkEmail", method = RequestMethod.POST)
    public Dto checkEmail(@RequestParam String phone) {
        Dto dto = phoneOrEmailRegex.regexPhoneOrEmail(phone);
        if (!dto.getMsg().equals("true")){
            return dto;
        }
        if (dmUserService.chekPhoneOrEmail(phone)) {
            return DtoUtil.returnSuccess("手机号或者邮箱可以使用");
        }
        return DtoUtil.returnFail("该邮箱或手机号已被使用", "11111");
    }
    @RequestMapping(value = "/p/code", method = RequestMethod.POST)
    public Dto code(@RequestParam String phone) {
        if (redisUtils.get(RedisKeys.getSysConfigKey(phone)) != null) {
            Long seconds = redisUtils.getSeconds(RedisKeys.getSysConfigKey(phone));
            return DtoUtil.returnFail("验证码频繁，请" + seconds + "秒后重试;", "1025");
        }
        if (checkEmail(phone).getMsg().equals("true")) {
            String id = phoneOrEmailRegex.getFourRandom();
            System.out.println("验证码:" + id);
            redisUtils.set(RedisKeys.getSysConfigKey(phone), id, 300);
            return DtoUtil.returnSuccess("成功发送验证码");
        }
        return null;
    }
    @RequestMapping(value = "/p/register", method = RequestMethod.POST)
    public Dto register(@RequestBody Jek jek) throws Exception {
        if (jek!=null){
            if (redisUtils.get(RedisKeys.getSysConfigKey(jek.getPhone())).equals("") || redisUtils.get(RedisKeys.getSysConfigKey(jek.getPhone())) == null){
                return DtoUtil.returnFail("验证码已过期，请重新发送","33333");
            }
            if (redisUtils.get(RedisKeys.getSysConfigKey(jek.getPhone())).equals(jek.getVcode())) {
                DmUser dmUser=new DmUser();
                dmUser.setPhone(jek.getPhone());
                if (jek.getPassword()!=null){
                    dmUser.setPassword(MD5.getMd5(jek.getPassword(),32));
                }else {
                    return DtoUtil.returnFail("请填写密码","1111");
                }
                if (dmUserService.insertDmUser(dmUser)>0) {
                    return DtoUtil.returnSuccess("注册成功");
                }else {
                    return DtoUtil.returnFail("注册失败","1111");
                }
            }else {
                return DtoUtil.returnFail("验证码错误","1111");
            }
        }else {
            return DtoUtil.returnFail("请完整填写信息","2222");
        }
    }
    @RequestMapping(value = "/user/msg", method = RequestMethod.POST)
    public Dto msg(@RequestParam String phone) {
        Dto dto = phoneOrEmailRegex.regexPhoneOrEmail(phone);
        if (!dto.getMsg().equals("true")){
            return dto;
        }
        if (dmUserService.chekPhoneOrEmail(phone)) {
            return DtoUtil.returnFail("手机号不存在，请先注册", "1025");
        }
        Long seconds=0L;
        if (redisUtils.get(RedisKeys.getSysConfigKey(phone)) != null) {
             seconds = redisUtils.getSeconds(RedisKeys.getSysConfigKey(phone));
            return DtoUtil.returnFail("验证码频繁，请" + seconds + "秒后重试;", "1025");
        }
            String id = phoneOrEmailRegex.getFourRandom();
            System.out.println("验证码:" + id);
            redisUtils.set(RedisKeys.getSysConfigKey(phone), id, 60);
            return DtoUtil.returnSuccess("成功发送验证码");
    }
        @RequestMapping(value = "/user/login", method = RequestMethod.POST)
        public Dto login_user(@RequestBody Jek jek) throws Exception {
            if (EmptyUtils.isEmpty(jek)) {
                return DtoUtil.returnFail("请完整填写信息", "2222");
            }
            if (redisUtils.get(RedisKeys.getSysConfigKey(jek.getPhone())).equals("") || redisUtils.get(RedisKeys.getSysConfigKey(jek.getPhone())) == null) {
                return DtoUtil.returnFail("验证码已过期，请重新发送", "33333");
            }
            if (redisUtils.get(RedisKeys.getSysConfigKey(jek.getPhone())).equals(jek.getVcode())) {
                Map<String, Object> map = new HashMap<>();
                map.put("phone", jek.getPhone());
                List<DmUser> dmUserListByMap = dmUserService.getDmUserListByMap(map);
                if (dmUserListByMap.size() == 0) {
                    return DtoUtil.returnFail("账号或验证码错误", "1006");
                }
                DmUser dmUser1 = dmUserListByMap.get(0);
                redisUtils.set(RedisKeys.getSysConfigKey(dmUser1.getPhone()), dmUser1);
                System.out.println("ip--------------------："+ t1.getHost());
                return DtoUtil.returnSuccess("登录成功", dmUser1);
            }else
                return DtoUtil.returnFail("验证码错误", "33333");
        }
}
